package com.framework;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: admin
 * @Description:
 * @Date: 2019/6/3 15:54
 */

public class FrameWorkMain {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //String baseDirName = FrameWorkMain.class.getClass().getResource("/").getPath();
        //String b = FrameWorkMain.class.getName();
        //Class c = FrameWorkMain.class;
        //System.out.println(c.getName());
        //File baseDir = new File(baseDirName);
        //System.out.println(baseDir);
        //System.out.println(b);
        //scan("/",baseDir);
        Map<String, Class> classMap = new HashMap<>();
        doScan(FrameWorkMain.class, classMap);
        classMap.forEach((className, classType) -> System.out.println(className));
        //System.out.println("----");
    }

    public static void doScan(Class appClass, Map<String, Class> classMap) throws ClassNotFoundException {
        String packagePath = appClass.getClass().getResource("/").getPath();
        String packageName = appClass.getPackage().getName();
        packagePath = packagePath + packageName.replace(".", "/");
        scanClass(packageName, packagePath, classMap);
    }

    public static void scanClass(String packageName, String packagePath, Map<String, Class> classMap) throws ClassNotFoundException {
        //System.out.println(packageName);
        //System.out.println(packagePath);
        File file = new File(packagePath);
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] subFiles = file.listFiles();
                for (File subFile : subFiles) {
                    if (subFile.isDirectory()) {
                        scanClass(packageName + "." + subFile.getName(), packagePath + "/" + subFile.getName(), classMap);
                    } else {
                        String subFileName = subFile.getName();
                        if (subFileName.indexOf(".class") > 0) {
                            String className = packageName + "." + subFileName.substring(0, subFileName.length() - 6);
                            classMap.put(className, Class.forName(className));
                        }
                    }
                }
            }
        }
    }

    public static String[] getClass(String packagePath) {
        StringBuilder stringBuilder = new StringBuilder();

        return null;
    }

    public static void scan(String packageName, File file) throws IOException {
        System.out.println(packageName);
        if (!file.exists()) return;
        if (file.isFile()) {
            String absolutePath = file.getAbsolutePath();
            String canonicalPath = file.getCanonicalPath();
            String fileName = file.getName();
            System.out.println(absolutePath);
            System.out.println(canonicalPath);
            System.out.println(fileName);
            return;
        }
        File[] files = file.listFiles();
        for (File f : files) scan(packageName + "." + file.getName(), f);
    }

}
